package com.steven.ourcontest.contest.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.steven.ourcontest.R
import com.steven.ourcontest.app.AppRouter
import com.steven.ourcontest.contest.bean.MatchInfo
import com.steven.ourcontest.utils.DateUtil

class ContestGuessAdapter(private val context: Context) :
    RecyclerView.Adapter<ViewHolder>() {
    val listData by lazy {
        ArrayList<MatchInfo>()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val inflater =
            LayoutInflater.from(context).inflate(R.layout.item_match_list, parent, false)
        return ViewHolder(inflater)
    }

    override fun getItemCount(): Int {
        return listData.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val itemData = listData[position]
        holder.apply {
            tv_match_name?.text = itemData.name
            itemData.belong?.let {
                tv_belong?.text = "主办方：${it}"
            }
            tv_signup_time?.text = "报名时间：${if (itemData.signUpStartTime != 0L && itemData.signUpEndTime != 0L) {
                DateUtil.timeStamp2Date(
                    itemData.signUpStartTime?.times(1000),
                    "yyyy.MM.dd"
                ) + "~" + DateUtil.timeStamp2Date(itemData.signUpEndTime?.times(1000), "yyyy.MM.dd")
            } else {
                "暂无"
            }}"
            tv_match_time?.text = "比赛时间：${if (itemData.matchStartTime != 0L && itemData.matchEndTime != 0L) {
                DateUtil.timeStamp2Date(
                    itemData.matchStartTime?.times(1000),
                    "yyyy.MM.dd"
                ) + "~" + DateUtil.timeStamp2Date(itemData.matchEndTime?.times(1000), "yyyy.MM.dd")
            } else {
                "暂无"
            }}"
            tv_readNum?.text = itemData.sum.toString()
            itemView.setOnClickListener {
                AppRouter.openMatchDetailActivity(context, itemData.id)
            }
        }
    }
}

class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val tv_match_name: TextView? = itemView.findViewById(R.id.tv_match_name)
    val tv_belong: TextView? = itemView.findViewById(R.id.tv_belong)
    val tv_signup_time: TextView? = itemView.findViewById(R.id.tv_signup_time)
    val tv_match_time: TextView? = itemView.findViewById(R.id.tv_match_time)
    val tv_readNum: TextView? = itemView.findViewById(R.id.tv_readNum)
}
